home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d11 / basmus.arc / SOUNDS.BAS < prev    next >
BASIC Source File  |  1990-09-29  |  5KB  |  121 lines

  1. 10 '            SOUNDS.BAS
  2. 20 '
  3. 30 '            By: Andrew Tuline
  4. 40 '
  5. 50 ' The sound generation on the IBM PC is performed mostly by the 8253 timer
  6. 60 'chip. This IC has 3 - 16 bit counters, each serving a different purpose
  7. 70 'on the system board.
  8. 80 '
  9. 90 '    Timer 0 is used for Dynamic Memory refresh.
  10. 100 '   Timer 1 is used for the time of day function.
  11. 110 '   Timer 2 is used for the speaker cassette interface.
  12. 120 '
  13. 130 '   The 8253 Timer is highly versatile and can be programmed for many
  14. 140 'different functions. These include a programmable rate generator, an
  15. 150 'event counter, a binary rate multiplier, real time clock, and others.
  16. 160 'The programmer may optinally change the mode of operation by addressing
  17. 170 'the control register. There are 2 registers for each counter, one for
  18. 180 'storing data to the timer and the other to read data from the timer.
  19. 190 'There are 6 modes of operation. Mode 3, the square wave generator will
  20. 200 'be discussed here. The control register is setup as follows:
  21. 210 '
  22. 220 '        -------------------------------------------------------------
  23. 230 'bit:    !  D7     D6   !  D5     D4   !  D3     D2     D1   !  D0   !
  24. 240 '        -------------------------------------------------------------
  25. 250 'def:    !  SC1    SC0  !  RL1    RL0  !  M2     M1     M0   !  BCD  !
  26. 260 '        -------------------------------------------------------------
  27. 270 '
  28. 280 'SC1,SC0 - Select the timer and must be 10 binary to select counter 2.
  29. 290 '
  30. 300 'RL1,RL0 - Read/Load
  31. 310 ' 0   0    Counter latching. When loaded, causes the present count to be
  32. 320 '          latched into a storage register for later reading.
  33. 330 ' 0   1    Read/Load LSB of counter only.
  34. 340 ' 1   0    Read/Load MSB of counter only.
  35. 350 ' 1   1    Read/Load LSB first then MSB.
  36. 360 '
  37. 370 'M2,M1,M0 - Select different modes. Must be 011 binary to select mode 3.
  38. 380 '
  39. 390 '  BCD   - Binary or Decimal counter.
  40. 400 '   0    - Sets counter to binary mode.
  41. 410 '   1    - Sets counter to decimal mode.
  42. 420 '
  43. 430 'Note: The larger the value loaded into the counter, the lower will be
  44. 440 'the resultant frequency. The number loaded sets the period of the
  45. 450 'tone which is the inverse of the frequency.
  46. 460 '
  47. 470 'Finally, the user must enable the speaker by setting a bit at I/O address
  48. 480 '61H (hexadecimal).
  49. 490 '
  50. 500 'Addresses
  51. 510 '
  52. 520 '42H - Counter 2 data register
  53. 530 '43H - Control register
  54. 540 '61H - Bit 1 enables disables the speaker.
  55. 550 '      Value of 4DH disables, 4FH enables.
  56. 560 '
  57. 570 'Examples
  58. 580 '
  59. 590 'Single Tone
  60. 600 OUT &H43,&HA6:'Binary 1010 0110
  61. 610 '              Read/Load MSB only
  62. 620 '              Mode 3
  63. 630 '              Counter 2
  64. 640 '              Binary Counter
  65. 650 OUT &H42,&H10:'Loads counter with 1000H
  66. 660 OUT &H61,&H4F
  67. 670 FOR I=1 TO 1000:NEXT
  68. 680 OUT &H61,&H4D:'disable speaker
  69. 690 '
  70. 700 'Increasing Frequency
  71. 710 OUT &H43,&HB7:'R/L LSB then MSB, Mode 3, BCD, Counter 2
  72. 720 OUT &H61,&H4F:'Enable speaker
  73. 730 FOR I=1000 TO 10 STEP -1:'Note: Decreasing value to be output
  74. 740 OUT &H42,I MOD 256:'LSB of I
  75. 750 OUT &H42,INT(I/256):'MSB of I
  76. 760 NEXT
  77. 770 '
  78. 780 'Weird Sounds
  79. 790 OUT &H43,&HA6:'R/L MSB, mode 3, counter 2, binary
  80. 800 FOR J=1 TO 20
  81. 810 FOR I=25 TO 5 STEP -1
  82. 820 OUT &H42,I
  83. 830 NEXT I
  84. 840 NEXT J
  85. 850 '
  86. 860 'Random Sounds
  87. 870 OUT &H43,&H96:'R/L LSB only, mode 3, counter 2, binary
  88. 880 FOR I=1 TO 200
  89. 890 OUT &H42,RND*255
  90. 900 NEXT I
  91. 910 OUT &H61,&H4D
  92. 920 '
  93. 930 'Assembly Language Random Number Generator
  94. 940 '
  95. 950 '1) Initialize
  96. 960 '1) Initialize      MOV     AL,0B6H        ;Mode 3, counter 2, binary
  97. 970 '                   OUT     43H,AL         ;R/L LSB then MSB
  98. 980 '                   MOV     AX,0FFFFH      ;store large number in counter
  99. 990 '                   OUT     42H,AX
  100. 1000 '
  101. 1010 '2) Read random #
  102. 1020 '                  MOV     AL,80H        ;counter 2,latch,rest don't care
  103. 1030 '                  OUT     43H,AL
  104. 1040 '                  IN      AX,42H
  105. 1050 '
  106. 1060 'I haven't tried the above in a program, but it should work reasonably.
  107. 1070 'And there you have it folks. A complete introductory to better sound
  108. 1080 'generation on your handy little PC. Last, but not least is a little
  109. 1090 'program to strobe your cassette port and output the data to the speaker,
  110. 1100 'written in assembly language. I have run this, and it works (sorta).
  111. 1110 '
  112. 1120 'TOP:      IN      AL,62H
  113. 1130 '          AND     AL,10H
  114. 1140 '          SHR     AL,1
  115. 1150 '          SHR     AL,1
  116. 1160 '          SHR     AL,1
  117. 1170 '          OR      AL,45H
  118. 1180 '          OUT     61H,AL
  119. 1190 '          JMP     TOP
  120. 1200 '
  121.